package com.ahua.mallchat.common.handler;

import com.ahua.mallchat.common.adapter.WXTextBuilder;
import com.ahua.mallchat.common.service.WxMsgService;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 关注事件
 */
@Component
public class SubscribeHandler extends AbstractHandler {

    @Resource
    private WxMsgService wxMsgService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService weixinService, WxSessionManager sessionManager) {
        String eventKey = wxMessage.getEventKey();
        //判断一下是否存在 eventKey 如果不存在 说明是自主关注公众号 给他返回个
        if (!StringUtils.hasText(eventKey)) {
            return WXTextBuilder.build("感谢关注", wxMessage);
        }

        return wxMsgService.scan(weixinService, wxMessage);
    }
}
